package com.edfu.core.commons.service.impl;

import static com.edfu.core.commons.exceptions.CommonExceptions.CATEGORY_NAME_NOT_PRESENT;
import static com.edfu.core.commons.exceptions.CommonExceptions.CATEGORY_NAME_NULL_OR_EMPTY;
import static com.edfu.core.commons.exceptions.CommonExceptions.CATEGORY_NOT_FOUND;
import static com.edfu.core.testmgt.exceptions.TestExceptions.USER_NAME_NULL_OR_EMPTY;
import static com.edfu.core.testmgt.exceptions.TestExceptions.USER_NOT_FOUND;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.edfu.core.commons.beans.Category;
import com.edfu.core.commons.exceptions.CategoryException;
import com.edfu.core.commons.exceptions.InvalidArgumentException;
import com.edfu.core.commons.persistence.api.ICategoryMgtPersistence;
import com.edfu.core.commons.persistence.api.IInterestsMgtPersistence;
import com.edfu.core.commons.persistence.entity.CategoryEntity;
import com.edfu.core.commons.persistence.entity.InterestsEntity;
import com.edfu.core.commons.service.api.IInterestsMgtService;
import com.edfu.core.commons.util.CategoryMgtBeanTransformer;
import com.edfu.core.identitymgt.persistence.api.IIdentityMgtPersistence;
import com.edfu.core.identitymgt.persistence.entity.UserEntity;
import com.edfu.core.identitymgt.util.IdentityMgtModelConverter;
import com.edfu.core.identitymgt.vo.User;

public class InterestMgtServiceImpl implements IInterestsMgtService {
	
	private static Logger logger = LoggerFactory.getLogger(InterestMgtServiceImpl.class);
	
	@Autowired
	private IIdentityMgtPersistence identityMgtPersistence;
	
	@Autowired
	private ICategoryMgtPersistence categoryMgtPersistence;
	
	@Autowired
	private IInterestsMgtPersistence interestsMgtPersistence;
	
	private UserEntity getUserEntityAndValidate(String userId) {
		
		if (StringUtils.isBlank(userId) || StringUtils.isEmpty(userId)) {
			logger.warn("UserId is Null or Empty");
			throw new InvalidArgumentException(USER_NAME_NULL_OR_EMPTY);
		}
		
		UserEntity ue = identityMgtPersistence.getUser(userId);
		
		if (ue == null) {
			logger.warn("User not found");
			throw new RuntimeException(USER_NOT_FOUND);
		}
		return ue;
	}
	
	private CategoryEntity getCategoryEntity(String categoryName) {
		if (StringUtils.isBlank(categoryName) || StringUtils.isEmpty(categoryName)) {
			logger.warn("Category name not present");
			throw new InvalidArgumentException(CATEGORY_NAME_NOT_PRESENT);
		}
		
		CategoryEntity tce = categoryMgtPersistence.getCategory(categoryName);
		
		if (tce == null) {
			logger.info("Category is not present");
			throw new CategoryException(CATEGORY_NOT_FOUND);
		}
		
		return tce;
	}

	@Override
	public void addInterests(String userId, String categoryName) {
		UserEntity ue = getUserEntityAndValidate(userId);
		CategoryEntity cce = getCategoryEntity(categoryName);
		
		interestsMgtPersistence.addInterests(ue, cce);
	}

	@Override
	public void addInterests(String userId, List<String> categoryNames) {
		UserEntity ue = getUserEntityAndValidate(userId);
		
		if (categoryNames == null || categoryNames.size() == 0) {
			throw new InvalidArgumentException(CATEGORY_NAME_NULL_OR_EMPTY);
		}
		
		for (String categoryName : categoryNames) {
			CategoryEntity ce = getCategoryEntity(categoryName);
			
			interestsMgtPersistence.addInterests(ue, ce);
		}		
	}

	@Override
	public List<Category> getUserInterests(String userId) {
		UserEntity ue = getUserEntityAndValidate(userId);
		List<InterestsEntity> userInterests = interestsMgtPersistence.getUserInterests(ue);
		List<Category> categories = new ArrayList<Category>();
		
		if (userInterests != null && userInterests.size() > 0) {
			for (InterestsEntity interestEntity : userInterests) {
				categories.add(CategoryMgtBeanTransformer.transformCategory(interestEntity.getCategory()));
			}
		}
		
		return categories;
	}

	@Override
	public List<User> getUsersInterestedInCategory(String categoryName) {
		CategoryEntity ce = getCategoryEntity(categoryName);
		List<InterestsEntity> categoryInterests = interestsMgtPersistence.getCategoryInterests(ce);
		List<User> users = new ArrayList<User>();
		
		if (categoryInterests != null && categoryInterests.size() > 0) {
			for (InterestsEntity interestEntity : categoryInterests) {
				users.add(IdentityMgtModelConverter.getUser(interestEntity.getUser()));
			}
		}
		
		return users;
	}	

}
